import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateDto.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/mk_card_container.dart';
import 'package:bxultimate/widgets/mk_empty.dart';
import 'package:bxultimate/widgets/mk_list_cell_item.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class StatisticsTaskTemplateList extends StatefulWidget {
  final String orgId;

  const StatisticsTaskTemplateList({super.key, required this.orgId});

  @override
  State<StatisticsTaskTemplateList> createState() =>
      _StatisticsTaskTemplateListState();
}

class _StatisticsTaskTemplateListState
    extends State<StatisticsTaskTemplateList> {
  List<OrganizationAnnouncementTemplateDto> _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getData();
  }

  getData() async {
    _dataList =
        await OrganizationRequest.getStatisticsTaskTeamplateList(1, 9999);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return _buildNav();
  }

  _buildNav() {
    return MkCardContainer(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding / 2),
      margin: EdgeInsets.only(
          top: 0,
          left: AppTheme.iDefaultPadding,
          right: AppTheme.iDefaultPadding),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MkListCellItem(
            textIcon: MkLoadImage(
                "https://minio.sxmbxt.com/static/img/my/order.png",
                width: 18),
            value: '创建新模板',
            press: () {
              Get.toNamed(RouterName.organizationStatisticsTemplateCreate);
            },
          ),
          Gaps.vGap12,
          Padding(
              padding: EdgeInsets.only(left: 12),
              child: AppTitle(
                title: '我管理的模板',
                textStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
              )),
          Gaps.vGap8,
          _dataList.length == 0 ? MkEmpty() : SizedBox(),
          ..._dataList.map((e) => Slidable(
                endActionPane: ActionPane(
                  motion: ScrollMotion(),
                  children: [
                    SlidableAction(
                      // An action can be bigger than the others.
                      onPressed: (context) {
                        deleteTemplate(e.id);
                      },
                      backgroundColor: Colors.redAccent,
                      foregroundColor: Colors.white,
                      icon: Icons.delete,
                      label: '删除',
                    ),
                  ],
                ),
                child: MkListCellItem(
                  value: '${e.templateName}',
                  press: () async {
                    await Get.toNamed(
                        RouterName.organizationStatisticsTemplateCreate,
                        arguments: e.id);
                    getData();
                  },
                ),
              ))
        ],
      ),
    );
  }

  void deleteTemplate(String? id) async {
    showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return TDAlertDialog(
          title: '确认删除么？',
          rightBtnAction: () async {
            Get.back();
            await OrganizationRequest.deleteTemplate(id!);
            SmartDialog.showNotify(msg: '删除成功', notifyType: NotifyType.success);
            getData();
          },
        );
      },
    );
  }
}
